const db = require("../../module/db/db.js")
const jsonParse = require('../../lib/jsonParse.js')
const axios = require("../../lib/params.js")
const jwt = require('jsonwebtoken');
const sd = require('silly-datetime')
const text = require("../json/sysMenu.json")
const text2 = require("../json/makeupInfo.json")
const makeupInfo = require('./goodsInfo').makeupInfo
const phoneInfo = require('./goodsInfo').phoneInfo



const users = db.getSchema("users")
const sysMenu = db.getSchema("sysMenu") // 手机
const usersInfo = db.getSchema("usersInfo") // 手机

process.on('unhandledRejection', error => {
  console.log('我帮你处理了', error.message);
})

/** 查询用户账号是否存在 */
function findUser(params) {
  return new Promise((resolve, reject) => {
    const userName = params.userName
    users.find({
      'userName': userName
    }).exec(function (err, data) {
      resolve(data)
    })
  })
}

/**查询用户账号密码是否匹配 */
function findPass(params) {
  return new Promise((resolve, reject) => {
    const passWord = params.passWord
    const userName = params.userName
    users.find({
      'userName': userName,
      'passWord': passWord
    }).exec(function (err, data) {
      resolve(data)
    })
  })
}

/** token生成 */
function createToken(rule, time) {
  return new Promise((resolve, reject) => {
    jwt.sign(rule, 'secret', {
      expiresIn: time || 3600
    }, (err, token) => {
      if (err) {
        resolve('')
      } else {
        resolve(token)
      }
    })
  })
}


function createMenuTree(data, subData){
  let arr = data.filter(e => e.parentId === subData.id)
  if(arr.length) {
    subData._doc.children = arr
    arr.forEach(el => {
      el._doc.children = createMenuTree(data, el)
    })
  } else {
    subData._doc.children = []
  }
  return arr
}

module.exports = {
  // 登录接口
  async signIn(request, response, handler) {
    let param = await axios.grabPostParams(request)
    findUser(param).then(res => {
      console.log(res, 88888)
      if (res.length) {
        findPass(param).then(res => {
          const rule = {
            userName: param.userName
          }
          createToken(rule).then(token => {
            jsonParse.sendResult(response, 200, {
              token: "dj" + token
            }, '登录成功')
          })
        })
      } else {
        jsonParse.sendResult(response, 0, '未查询到该用户', )
      }
    })
  },
  
  // 获取登录用户基本信息
  async getUserInfo(request, response, handler){
    let userName = await axios.grabHeadParams(request)
    usersInfo.findOne({
      'userName': userName,
    }).exec(function (err, data) {
      jsonParse.sendResult(response, 200, data, '登录成功')
    })
  },

  // 重新登录
  async resetToken(request, response, handler) {
    let token = request.headers['x-token'].replace('dj', '');
    const decodeResult = jwt.decode(token)
    const rule = {
      userName: decodeResult.userName
    }
    createToken(rule, 3600).then(token => {
      jsonParse.sendResult(response, 200, {
        token: "dj" + token
      }, '重新登录成功')
    })
  },

  // 退出登录
  async loginOut(request, response, handler) {
    let param = await axios.grabPostParams(request)
  },

  // 注册接口
  async signUp(request, response, handler) {
    let param = await axios.grabPostParams(request)
    if (!param.userName) {
      jsonParse.sendResult(0, '用户名为空', )
    }
    if (!param.passWord) {
      jsonParse.sendResult(0, '密码为空', )
    }
    findUser(param).then(res => {
      if (res.length) {
        jsonParse.sendResult(response, 0, '该用户已存在', )
      } else {
        let user = new users({
          userName: param.userName,
          passWord: param.passWord,
          createTime: sd.format(new Date(), 'YYYY-MM-DD HH:mm:ss'),
        })
        user.save(function (err, res) {
          if (err) {
            jsonParse.sendResult(0, '该用户已存在', )
          } else {
            let usersInfo = new usersInfo({
              userName: param.userName,
              birth: '2021-05-06',
              type: param.type,
              vipLv: '1',
            })
            usersInfo.save((err, res) => {
              jsonParse.sendResult(response, 200, '注册成功', )
            })
            
          }
        })
      }
    })
  },

  // 导入菜单
  async exportInMenu(request, response, handler) {
    sysMenu.insertMany(text).then(function (err, data) {
      jsonParse.sendResult(response, 200, '添加成功', )
    })
  },
  
  // 获取菜单信息
  async getMenuInfo(request, response, handler){
    sysMenu.find().exec(function (err, data) {
      let temp = data.filter(e => !e.parentId || e.parentId === '0')
      temp.forEach(el => {
        createMenuTree(data, el)
      })
      jsonParse.sendResult(response, 200, temp)
    })
  },
}


